package com.example.langchanin4jdemo1.controller;

import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.agent.tool.ToolSpecifications;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.UserMessage;

import java.time.LocalDateTime;

public class AIServiceDemo3 {
    interface Assistant {
        String chat(@MemoryId Long memoryId, @UserMessage String message);
    }

    @Tool("获取当前日期")
    public static String dateUtil(){
        return LocalDateTime.now().toString();
    }

    public static void main(String[] args) throws Exception{
        ChatLanguageModel model = QwenChatModel.builder()
                .apiKey("sk-875dd6ef14244431acdc7ccb974f5bfe")
                .modelName("qwen-max")
                .build();

        ToolSpecification toolSpecification = ToolSpecifications.toolSpecificationFrom(AIServiceDemo3.class.getMethod("dateUtil"));

        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(model)
//                .chatMemory(chatMemory)
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                .tools(toolSpecification)
                .build();

        System.out.println(assistant.chat(1L, "你好，我是詹姆斯"));
        System.out.println(assistant.chat(1L, "我的名字是什么"));

        System.out.println(assistant.chat(1L, "你好，我是杜兰特"));
        System.out.println(assistant.chat(1L, "我的名字是什么"));
    }
}
